<template>
  <div>
    <Nav></Nav>
    <div class="btnBox">
      <el-button type="primary" class="addBtn" @click="add">添加</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
      <el-table-column prop="value" label="项目类型" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="250px">
        <template slot-scope="scope">
          <el-button type="primary" size="medium" @click="cmdEdit(scope.row)">修改</el-button>
          <el-button type="danger" size="medium" @click="cmdDel(scope.row.orunid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <add-dialog :show="showAddDialog" @closeDlg="closeAddDialog"></add-dialog>
    <edit-dialog :show="showEditDialog" :current="current" @closeDlg="closeEditDialog"></edit-dialog>
  </div>
</template>
<script>
import { getAllProjectType, deleteProjectType } from '@/api/api'
import AddDialog from './add'
import EditDialog from './edit'
import nav from '@/components/navigation'
export default {
  components: {
    Nav: nav,
    'add-dialog': AddDialog,
    'edit-dialog': EditDialog
  },
  data () {
    return {
      tableData: [],
      current: {},
      showAddDialog: false,
      showEditDialog: false,
    }
  },
  mounted () {
    this.getProjectType()
  },
  methods: {
    getProjectType () {
      getAllProjectType().then(res => {
        // console.log(res)
        if (res.code == 200) {
          this.tableData = res.data
        }
      }).catch((err) => { })
    },
    add () {
      this.showAddDialog = true
    },
    cmdEdit (row) {
      this.current = row
      this.showEditDialog = true
    },
    cmdDel (id) {
      this.confirm('是否确认删除!', () => {
        deleteProjectType({ orunid: id }).then(res => {
          console.log(res)
          if (res.code == 200) {
            swal('提示', '删除成功', 'success')
            this.getProjectType()
          } else {
            swal('错误', res.message || '删除失败', 'success')
          }
        }).catch((err) => {
          swal('错误', '网络连接错误', 'error')
        })
      })
    },
    closeAddDialog () {
      this.showAddDialog = false
      this.getProjectType()
    },
    closeEditDialog () {
      this.showEditDialog = false
      this.getProjectType()
    }
  }
}
</script>
<style lang="less" scoped>
.btnBox {
  display: flex;
  justify-content: flex-end;
  .addBtn {
    margin: 5px 10px 5px 0;
  }
}
</style>